#include "downloadHeaders/downloadviewh/downloaditemwidget.h"
#include "ui_downloaditemwidget.h"
#include "downloadHeaders/downloadcontrollerh/downloaditemcontroller.h"
#include <QFileInfo>
downloaditemwidget::downloaditemwidget(downloaditemcontroller *controller, QWidget *parent) :
    QWidget(parent),
    controller(controller),
    time(),
    ui(new Ui::downloaditemwidget)
{
    ui->setupUi(this);
    time.start();

    QIcon icon = controller->icon();
    QPixmap m_pic = icon.pixmap(icon.actualSize(QSize(20, 20)));//size自行调整
    ui->icon->setPixmap(m_pic);


    ui->title->setText(controller->setTitle());
    setStyleSheet("background-color:rgb(250, 252, 255, 0);\
                ");
    ui->progress->setStyleSheet("QProgressBar{background:white;} QProgressBar::chunk{background:#0997F7}");

    setStyleSheet(
                     "QPushButton{"
                     "border-radius:3px;"                   //边框圆角半径像素
                     "background-color:rgb(250, 252, 255, 0);"
                     "}"
                     //鼠标按下样式
                     "QPushButton:pressed{"
                     "background-color:rgb(107,107,107);"
                     "}"
                     //鼠标悬停样式
                     "QPushButton:hover{"
                     "background-color:rgb(138, 138, 138);"
                     "}");

    ui->wall->setStyleSheet("background-color:qlineargradient(spread:pad,x1:0, y1:0, x2:1, y2:0,stop:0 rgba(0,0,0,0%),stop:0.8 rgb(220, 232, 241),stop:1 rgb(220, 232, 241))");
    ui->wall->hide();
    connect(controller->downloadRequest, &QWebEngineDownloadRequest::totalBytesChanged, this, &downloaditemwidget::updateWidget);
    connect(controller->downloadRequest, &QWebEngineDownloadRequest::receivedBytesChanged, this, &downloaditemwidget::updateWidget);
    connect(controller->downloadRequest, &QWebEngineDownloadRequest::stateChanged, this, &downloaditemwidget::updateWidget);
    connect(controller->downloadRequest, &QWebEngineDownloadRequest::isPausedChanged, this, &downloaditemwidget::updateWidget);


    connect(ui->cancel, &QPushButton::clicked, this->controller, &downloaditemcontroller::cancel);
    connect(ui->cancel, &QPushButton::clicked, this, &downloaditemwidget::changeDeletedStyle);

    ui->cancel->setIcon(QIcon(":/resource/imgs/cancel.png"));

    connect(ui->resume, &QPushButton::clicked, this->controller, &downloaditemcontroller::resume);
    ui->resume->setIcon(QIcon(":/resource/imgs/continue.png"));

    connect(ui->pause, &QPushButton::clicked, this->controller, &downloaditemcontroller::pause);
    ui->pause->setIcon(QIcon(":/resource/imgs/pause.png"));

    connect(ui->openfolder, &QPushButton::clicked, this->controller, &downloaditemcontroller::openfolder);
    ui->openfolder->setIcon(QIcon(":/resource/imgs/folder.png"));

    connect(ui->deletefile, &QPushButton::clicked, this->controller, &downloaditemcontroller::deletefile);
    connect(ui->deletefile, &QPushButton::clicked, this, &downloaditemwidget::changeDeletedStyle);

    ui->deletefile->setIcon(QIcon(":/resource/imgs/delete.png"));

    ui->cancel->setVisible(false);
    ui->pause->setVisible(false);
    ui->restart->setVisible(false);
    ui->resume->setVisible(false);
    ui->deletefile->setVisible(false);
    ui->openfolder->setVisible(false);



}
void downloaditemwidget::changeDeletedStyle()
{
    ui->title->setStyleSheet("text-decoration:line-through;");
    if(ui->label->text() != QString::fromLocal8Bit("cancelled"))
        ui->label->setText("deleted");
}
void downloaditemwidget::enterEvent(QEnterEvent *event)
{
    if (controller->fileExists())//如果没有被删除
    {
        updateWidget();
        ui->wall->show();
    }
}

void downloaditemwidget::leaveEvent(QEvent *)
{
    ui->cancel->setVisible(false);
    ui->pause->setVisible(false);
    ui->restart->setVisible(false);
    ui->resume->setVisible(false);
    ui->deletefile->setVisible(false);
    ui->openfolder->setVisible(false);
    ui->wall->hide();
}


void downloaditemwidget::updateWidget()
{
    qreal totalBytes = controller->getTotalBytes();
    qreal receivedBytes = controller->getReceivedBytes();
    qreal bytesPerSecond = receivedBytes / time.elapsed() * 1000;

    int  restTime = (totalBytes - receivedBytes) / bytesPerSecond;
    qDebug()<<receivedBytes<<" "<<restTime<<" "<<controller->secsToTime(restTime)<<"\n";

    switch (controller->downloadRequest->state()) {
    case QWebEngineDownloadRequest::DownloadRequested:
        Q_UNREACHABLE();

        break;
    case QWebEngineDownloadRequest::DownloadInProgress:

        if (totalBytes >= 0)
        {
            ui->progress->setValue(qRound(100 * receivedBytes / totalBytes));
            ui->progress->setDisabled(false);
                        ui->label->setText(
                            tr("%1/%2 - %3/s - restime: %4")
                            .arg(controller->withUnit(receivedBytes))
                            .arg(controller->withUnit(totalBytes))
                            .arg(controller->withUnit(bytesPerSecond))
                            .arg(controller->secsToTime(restTime)));
        }
        ui->deletefile->setVisible(false);
        ui->openfolder->setVisible(false);
        ui->restart->setVisible(false);
        //鼠标在当前才show
        if (ui->wall->geometry().contains(this->mapFromGlobal(QCursor::pos())))
        {ui->cancel->show();}

        if(controller->isPaused())
        {
            ui->pause->setVisible(false);
            if (ui->wall->geometry().contains(this->mapFromGlobal(QCursor::pos())))
            {ui->resume->setVisible(true);}
            ui->label->setText("paused");
        }
        else
        {
            if (ui->wall->geometry().contains(this->mapFromGlobal(QCursor::pos())))
            {ui->pause->setVisible(true);}
            ui->resume->setVisible(false);
        }

        break;
    case QWebEngineDownloadRequest::DownloadCompleted:
        ui->progress->setValue(100);
        ui->progress->setVisible(false);
        ui->label->setText("completed");
        controller->insert();
//        ui->label->setOpenExternalLinks(true);
//        connect(ui->label, SIGNAL(ui->label->linkActivated("open file")), this->controller, SLOT(downloaditemcontroller::openfile));
        ui->cancel->setVisible(false);
        ui->pause->setVisible(false);
        ui->restart->setVisible(false);
        ui->resume->setVisible(false);
        if (ui->wall->geometry().contains(this->mapFromGlobal(QCursor::pos())))
        {
            ui->deletefile->setVisible(true);
            ui->openfolder->setVisible(true);
        }

        break;
    case QWebEngineDownloadRequest::DownloadCancelled:
        ui->progress->setValue(0);
        ui->label->setText("cancelled");
        controller->insert();
        ui->progress->setVisible(false);
        ui->cancel->setVisible(false);
        ui->pause->setVisible(false);
        //ui->restart->setVisible(true);
        ui->resume->setVisible(false);
        if (ui->wall->geometry().contains(this->mapFromGlobal(QCursor::pos())))
            ui->deletefile->setVisible(true);
        ui->openfolder->setVisible(false);
        break;
    case QWebEngineDownloadRequest::DownloadInterrupted:
        ui->progress->setValue(0);
        ui->progress->setDisabled(true);
        ui->progress->setFormat(
                    tr("interrupted: %1")
                    .arg(controller->downloadRequest->interruptReasonString()));
        break;
    }




}
downloaditemwidget::~downloaditemwidget()
{
    delete ui;
}
